package com.delivery.service.impl;

import com.delivery.constant.MessageConstant;
import com.delivery.dto.CategoryPageQueryDTO;
import com.delivery.dto.UpdateOrAddCategoryDTO;
import com.delivery.entity.Category;
import com.delivery.exception.DeletionNotAllowedException;
import com.delivery.mapper.CategoryMapper;
import com.delivery.mapper.DishMapper;
import com.delivery.mapper.SetmealMapper;
import com.delivery.result.PageResult;
import com.delivery.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void updateCategory(UpdateOrAddCategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateCategory(category);
    }

    @Override
    public PageResult categoryPageQuery(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<Category> page = categoryMapper.categoryPageSelect(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void enableOrDisableCategory(String status, Long id) {
        Category category = new Category().builder()
                .id(id)
                .status(Integer.parseInt(status))
                .build();
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateCategory(category);
    }

    @Override
    public void addCategory(UpdateOrAddCategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.insertCategory(category);
    }

    /**
     * 删除分类
     * @param id 分类id
     */
    @Override
    public void deleteCategory(Long id) {
        // 需要注意：删除分类时，需要判断该分类下是否有关联的菜品或者套餐
        Integer count = dishMapper.countRecordsByCategoryId(id);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        count = setmealMapper.countRecordsByCategoryId(id);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        categoryMapper.deleteCategory(id);
    }

    /**
     * 查询指定类型的分类
     * @param type 分类类型
     * @return 分类列表
     */
    @Override
    public List<Category> findCategoriesByType(Integer type) {
        List<Category> categories = categoryMapper.selectCategoriesByType(type);
        return categories;
    }

}
